package com.wisdomstar.common.utils.minio;

import io.minio.*;
import io.minio.errors.*;
import io.minio.http.Method;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @author RinMi
 * @since 2023/6/2
 */

@Component
public class MinioClientUtil {

    @Resource
    private MinioProp properties;

    private static MinioClient minioClient;

    @Value("${minio.endpoint}")
    private String url;

    /**
     * 初始化minio client
     */
    private void init(String bucket) throws NoSuchAlgorithmException, InvalidKeyException, IOException,
            InvalidResponseException, InsufficientDataException, ServerException, ErrorResponseException,
            XmlParserException, InternalException {
        if (null != minioClient) {
            return;
        }
        minioClient = MinioClient.builder()
                .endpoint(url)
                .credentials(properties.getAccessKey(), properties.getSecretKey())
                .build();
        makeBucket(bucket);
    }

    /**
     * 创建自定义的存储桶
     */
    public static void makeBucket(String bucket) throws IOException, InvalidKeyException, InvalidResponseException,
            InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException,
            ErrorResponseException {
        boolean bucketExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
        if (!bucketExist) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
        }
    }

    /**
     * 上传文件
     */
    public String uploadFile(String bucket, String filename, InputStream ins, long fileSize, String contentType)
            throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException,
            NoSuchAlgorithmException, ServerException, InternalException, XmlParserException,
            ErrorResponseException {
        init(bucket);
        makeBucket(bucket);
        filename = bucket + "_" + System.currentTimeMillis() + filename.substring(filename.lastIndexOf("."));
        PutObjectArgs.Builder putObjectArgsBuilder = PutObjectArgs.builder()
                .bucket(bucket)
                .contentType(contentType)
                .object(filename)
                .stream(ins, fileSize, 100 * 1024 * 1024);
        minioClient.putObject(putObjectArgsBuilder.build());
        return filename;
    }

    /**
     * 下载文件
     */
    public InputStream downloadFile(String bucket, String filename)
            throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException,
            NoSuchAlgorithmException, ServerException, InternalException, XmlParserException,
            ErrorResponseException {
        init(bucket);
        GetObjectArgs.Builder getObjectArgsBuilder = GetObjectArgs.builder()
                .bucket(bucket)
                .object(filename);
        return minioClient.getObject(getObjectArgsBuilder.build());
    }

    /**
     * 获取路径
     */
    private String getMinioUrl(String bucket, String filename) throws IOException,
            NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException,
            ServerException, XmlParserException, InternalException, InsufficientDataException,
            ErrorResponseException {
        if (null == minioClient) {
            init(bucket);
        }
        GetPresignedObjectUrlArgs build = GetPresignedObjectUrlArgs.builder()
                .method(Method.GET)
                .bucket(bucket)
                .object(filename)
//                .expiry(60 * 60 * 24)   //生成的预签名url可访问的有效时间，最大期限7天
                .build();
        return minioClient.getPresignedObjectUrl(build);
    }

    /**
     * 删除文件
     */
    public void delete(String bucket, String filename) throws IOException, InvalidKeyException, InvalidResponseException,
            InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException,
            ErrorResponseException {
        init(bucket);
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(filename).build());
    }
}
